﻿Imports OpenDelib.ServiceDeliberations

Public Class OpenDelibPage
    Inherits System.Web.UI.Page

    Public Shadows ReadOnly Property Master As OpenDelib
        Get
            Return CType(MyBase.Master, OpenDelib)
        End Get
    End Property

    'Init de la page (js + css)
    Protected Overrides Sub OnPreInit(e As EventArgs)

        Dim appPath As String = Request.ApplicationPath
        If appPath = "/" Then
            appPath = ""
        End If

        AddJS(Page, "jquery", appPath & "/Scripts/jquery-2.1.3.min.js")
        AddJS(Page, "jquery-ui", appPath & "/Scripts/jquery-ui.min.js")
        AddJS(Page, "dropzone", appPath & "/Scripts/dropzone.js")
        AddJS(Page, "datetimepicker", appPath & "/Scripts/jquery-ui-timepicker-addon.js")
        AddJS(Page, "datetimepicker-fr", appPath & "/Scripts/jquery-ui-timepicker-fr.js")
        AddJS(Page, "notify", appPath & "/Scripts/jquery.noty.packaged.min.js")
        AddJS(Page, "combos", appPath & "/Scripts/combos.js")

        If Tools.GetInstance.getToken <> "" Then
            AddJS(Page, "scriptMenu", appPath & "/Scripts/scriptMenu.js")
        End If
        'If Tools.GetInstance.isConnected Then
        'AddJS("scriptMenu", appPath & "/Scripts/scriptMenu.js")
        'End If

        MyBase.OnPreInit(e)
    End Sub

    Protected Overrides Sub OnInit(e As EventArgs)

        Dim appPath As String = Request.ApplicationPath
        If appPath = "/" Then
            appPath = ""
        End If

        AddCss(Page, appPath & "/Css/Autres/jquery-ui.css")
        AddCss(Page, appPath & "/Css/Banniere.css")
        AddCss(Page, appPath & "/Css/Controls.css")
        AddCss(Page, appPath & "/Css/Form.css")
        AddCss(Page, appPath & "/Css/Menu.css")
        AddCss(Page, appPath & "/Css/Grid.css")
        AddCss(Page, appPath & "/Css/Popup.css")
        AddCss(Page, appPath & "/Css/Text.css")
        AddCss(Page, appPath & "/Css/Autres/jquery-ui-timepicker-addon.css")
        AddCss(Page, appPath & "/Css/Autres/animate.css")

        MyBase.OnInit(e)
    End Sub

    Protected Overrides Sub OnError(e As EventArgs)
        MyBase.OnError(e)
        DisconnectionRedirect()
    End Sub

    Protected Overrides Sub OnLoadComplete(e As EventArgs)
        If IO.Path.GetFileName(Request.Path).ToLower <> "default.aspx" Then DisconnectionRedirect()
        MyBase.OnLoadComplete(e)
    End Sub

    Public Shared Sub AddJS(page As Page, key As String, jsFile As String)
        page.ClientScript.RegisterClientScriptInclude(key, jsFile)
    End Sub

    Public Shared Sub AddCss(page As Page, css As String)
        Dim lnk As New HtmlLink()
        lnk.Href = css
        lnk.Attributes("rel") = "stylesheet"
        lnk.Attributes("type") = "text/css"
        page.Header.Controls.Add(lnk)
    End Sub

    Public Sub NotifyErreur(erreur As String)
        Tools.NotifyErreur(Me, erreur)
    End Sub

    Public Sub NotifySuccess(msg As String)
        Tools.NotifySuccess(Me, msg)
    End Sub

    Public Sub NotifyMsg(msg As String)
        Tools.NotifyMsg(Me, msg)
    End Sub


    Protected Sub DisconnectionRedirect()
        If HttpContext.Current.Session IsNot Nothing Then
            If String.IsNullOrEmpty(Tools.GetInstance.getToken) Then
                If Request.QueryString("deco") <> "1" Then
                    Tools.Encrypt(Request.RawUrl)
                    Response.Redirect(Request.ApplicationPath & "/default.aspx?deco=1&url=" & Tools.Encrypt(Request.RawUrl))
                End If
            Else
                Dim ex As Exception = Server.GetLastError
                If ex IsNot Nothing Then NotifyErreur(ex.Message)
            End If
        End If
    End Sub

    <System.Web.Services.WebMethod>
    Public Shared Sub SaveOpenedMenuId(menuClientId As String)
        HttpContext.Current.Session("OpenedMenuClientId") = menuClientId
    End Sub
    <System.Web.Services.WebMethod>
    Public Shared Function ReadOpenedMenuId() As String
        If HttpContext.Current.Session("OpenedMenuClientId") Is Nothing Then Return ""
        Return CStr(HttpContext.Current.Session("OpenedMenuClientId"))
    End Function

    <System.Web.Services.WebMethod()>
    <System.Web.Script.Services.ScriptMethod()>
    Public Shared Function ChargerVilles(codePostal As String) As List(Of ServiceParametre.OIItem)
        Using svc As New ServiceParametre.ParametresClient
            Dim params As New ServiceParametre.ListeParametreParameters
            params.Authentication = New ServiceParametre.AuthenticationParameters
            params.Authentication.Token = Tools.GetInstance.getToken
            params.EntiteParametrage = 38
            params.Param2 = codePostal

            Try
                Dim results As ServiceParametre.OIItemListResult = svc.ListeParametre(params)
                If results IsNot Nothing AndAlso results.OK Then
                    Dim autre As New ServiceParametre.OIItem()
                    autre.Code = "¤"
                    autre.Libelle = "Autre ville..."
                    results.Liste.Add(autre)
                    Return results.Liste
                End If
            Catch ex As Exception

            End Try
        End Using

        Return New List(Of ServiceParametre.OIItem)
    End Function

End Class
